import datetime
import hashlib
import base64
import requests
import json


class YunTongXin():

    base_url = 'https://app.cloopen.com:8883'

    def __init__(self, accountSid, accountToken, appId, templateId):

        self.accountSid = accountSid    #账户ID
        self.accountToken = accountToken  # 授权令牌
        self.appId = appId    # 应用ID
        self.templateId = templateId   # 模板ID

    def get_request_url(self, sig):
        # 完整的URL创建==base_url + 业务URL
        # /2013-12-26/Accounts/{accountSid}/SMS/{funcdes}?sig={SigParameter}
        self.url = self.base_url+'/2013-12-26/Accounts/%s/SMS/TemplateSMS?sig=%s'%(self.accountSid, sig)
        return self.url

    def get_timestamp(self):
        # 生成时间戳
        return datetime.datetime.now().strftime('%Y%m%d%H%M%S')


    def get_sigparameter(self, timestamp):
        # 生成URL的sig
        s = self.accountSid+self.accountToken+timestamp
        m = hashlib.md5()
        m.update(s.encode())
        return m.hexdigest().upper()

    def get_request_header(self, timestamp):
        # 生成请求头
        s = self.accountSid + ':' + timestamp
        auth = base64.b64encode(s.encode()).decode()
        return {
            'Accept': 'application/json',
            'Content-Type': 'application/json;charset=utf-8',
            'Authorization': auth
        }


    def get_request_body(self, phone, code):

        return {
            "to": phone,
            "appId": self.appId,
            'templateId': self.templateId,
            'datas': [code, "3"]
        }

    def request_api(self, url, header, body):
        res = requests.post(url, headers=header, data=body)
        return res.text



    def run(self, phone,code):
        # 获取时间戳
        timestamp = self.get_timestamp()
        # 生成签名
        sig = self.get_sigparameter(timestamp)
        # 生成业务url
        url = self.get_request_url(sig)
        # print(url)
        header = self.get_request_header(timestamp)
        # print(header)
        # 生成请求体
        body = self.get_request_body(phone, code)
        # 发请求
        data = self.request_api(url, header, json.dumps(body))
        return data



# 测试
if __name__ == '__main__':


    # accountSid,accountToken,appId,templateId
    config = {
        "accountSid": "8a216da88185a9c00181a9046c1a0767",
        "accountToken": "aa68731ea7ce487caf1eb8e7c686eea9",
        "appId": "8a216da88185a9c00181a9046d22076e",
        "templateId": "1"
    }

    yun = YunTongXin(**config)
    res = yun.run("18290922922", "112233")
    print(res)